<?php

	$hash = Jpublic::get('formhash');

	$type = Jpublic::get('type');

	if (JAdmin_core::HASH($hash)) {

		$id = Jpublic::get('id');
		
		$table = PRE . 'plugin_menu';
		
		$form_data = Jdb::data(array(
			table => $table,
			type => 'POST'
		));
		
		if ($id == 0) {
			
			$maxLevel = Jdb::select("SELECT Max(pmLevel) as maxPmLevel FROM `".PRE."plugin_menu` WHERE pmParentID = 0");

			$pmLevel = $maxLevel['maxPmLevel'];

			if ($pmLevel == '') {
				
				$pmLevel = '000000';
				
			}
			
			$form_data['pmLevel'] = common::add0($pmLevel + 1000);
			
			$form_data['pmSort'] = (int)$from_data['pmSort'];
			
			Jdb::insert($table, $form_data);
			
			common::log('insert', '添加应用类别');
			
			$HTML -> tip_html('insert');
			
		}
		
		$parent = Jdb::select("SELECT * FROM `".PRE."plugin_menu` WHERE webosPluginMenuID = $id");
		
		$parentID = $parent['webosPluginMenuID'];
		
		if ($type == 'update') {
			
			Jdb::update($table, $form_data, "WHERE webosPluginMenuID = $id");
			
			common::log('update', '修改应用类别名称');
			
			$HTML -> tip_html('update');
			
		} else if ($type == 'insert') {
			
			$maxLevel = Jdb::select("SELECT Max(pmLevel) as maxPmLevel FROM `".PRE."plugin_menu` WHERE pmParentID = $parentID");
			
			if ($maxLevel['maxPmLevel'] == '') {
				
				$maxLevel = Jdb::select("SELECT pmLevel as maxPmLevel FROM `".PRE."plugin_menu` WHERE webosPluginMenuID = $parentID");
				
			}
			
			$form_data['pmParentID'] = $id;
			
			$form_data['pmLevel'] = common::add0(++$maxLevel['maxPmLevel']);
			
			$form_data['pmSort'] = (int)$from_data['pmSort'];
			
			Jdb::insert($table, $form_data);
			
			common::log('insert', '添加应用类别');
			
			$HTML -> tip_html('insert');
			
		}
		
	}

?>